using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace cb0tServer
{
    class DictionaryDefinition
    {
        private Thread thread;
        private bool busy = false;

        private ushort vroom = 0;
        private String query = String.Empty;

        public void Query(ushort vroom, String query)
        {
            if (busy)
            {
                UserPool.Broadcast(vroom, ServerOutboundPackets.AnnoucePacket("wait a few seconds and then try again"));
                return;
            }

            this.vroom = vroom;
            this.query = query;
            this.thread = new Thread(new ThreadStart(this.Work));
            this.thread.Start();
        }

        private String removeEntities(String str)
        {
            String _str = str;
            _str = _str.Replace("&quot;", "\"");
            _str = _str.Replace("&amp;", "&");
            _str = _str.Replace("&#160;", " ");
            _str = _str.Replace("&lt;", "<");
            _str = _str.Replace("&gt;", ">");
            return _str;
        }

        private String removeHtml(String unfiltered)
        {
            String result = unfiltered;

            while (((result.IndexOf("<") > -1)
                && (result.IndexOf(">") > -1))
                && (result.IndexOf("<") < result.IndexOf(">")))
            {
                result = result.Substring(0, result.IndexOf("<"))
                    + result.Substring(result.IndexOf(">") + 1);
            }

            return result;
        }

        private void Work()
        {
            this.busy = true;

            try
            {
                StreamReader s = new StreamReader(WebRequest.Create("http://www.thefreedictionary.com/" + Uri.EscapeDataString(this.query)).GetResponse().GetResponseStream());
                List<String> items = new List<String>();

                while (!s.EndOfStream)
                {
                    String str = s.ReadLine();
                    int count = 0;

                    if (str.IndexOf("Word not found in the Dictionary") > -1)
                    {
                        items.Clear();
                        break;
                    }

                    if (str.IndexOf("class=\"ds-list\">") > -1)
                    {
                        while (str.IndexOf("class=\"ds-list\">") > -1)
                        {
                            str = str.Substring(str.IndexOf("class=\"ds-list\">") + 16);
                            int end = str.IndexOf("</div>");
                            if (end == -1) break;
                            String print = str.Substring(0, end);
                            print = removeEntities(removeHtml(print));

                            if (print.Substring(1).StartsWith(". "))
                                print = print.Substring(print.IndexOf(". ") + 1);

                            if (print.Length > 0)
                            {
                                items.Add(print);
                                if (count++ > 2) break;
                            }
                        }

                        break;
                    }

                    if (str.IndexOf("class=\"ds-single\">") > -1)
                    {
                        while (str.IndexOf("class=\"ds-single\">") > -1)
                        {
                            str = str.Substring(str.IndexOf("class=\"ds-single\">") + 18);
                            int end = str.IndexOf("</div>");
                            if (end == -1) break;
                            String print = str.Substring(0, end);
                            print = removeEntities(removeHtml(print));

                            if (print.Substring(1).StartsWith(". "))
                                print = print.Substring(print.IndexOf(". ") + 1);

                            if (print.Length > 0)
                            {
                                items.Add(print);
                                if (count++ > 2) break;
                            }
                        }

                        break;
                    }
                }

                s.Close();

                if (items.Count == 0)
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("Unable to find a dictionary definition for " + this.query + "!"));
                }
                else
                {
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket("10Dictionary Definition for: 14" + this.query));
                    UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(" "));

                    bool def_col = true;

                    for (int i = 0; i < items.Count; i++)
                    {
                        UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket((def_col ? "06" : "03") + (i + 1) + ". " + items[i]));
                        def_col = !def_col;

                        if (i < (items.Count - 1))
                            UserPool.Broadcast(this.vroom, ServerOutboundPackets.AnnoucePacket(""));
                    }
                }
            }
            catch { }

            this.busy = false;
        }
    }
}
